//
//  MRegisterViewController.m
//  NeusoftHBSJSober
//
//  Created by Apple-21 on 15/11/4.
//  Copyright (c) 2015年 com.neusoftsober. All rights reserved.
//

#import "MRegisterViewController.h"
#import "MViewUtil.h"
#import "MRegistService.h"
@interface MRegisterViewController () <UITextFieldDelegate>{

    UITextField *accountTxt;
    UITextField *pwdTxt;
    
    UITextField *confirmPwdTxt;
    
    UIButton *registerBtn;
}
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
- (IBAction)backBtnAction:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIView *contentView;

@end

@implementation MRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setBackgroundEffect];
    [self setContentView];
}
- (void)setContentView {
    
    UIImageView *myImageView = [[UIImageView alloc]initWithFrame:CGRectMake((SCREENWIDTH-70)/2, 30, 70, 70)];
    [self.view addSubview:myImageView];
    myImageView.image = [UIImage imageNamed:@"icon_p"];
    [myImageView.layer setCornerRadius:35];
    [myImageView.layer setMasksToBounds:YES];
    
    accountTxt = [[UITextField alloc]initWithFrame:CGRectMake(SCREENWIDTH/5/2, 60, SCREENWIDTH*4/5, 44)];
    [accountTxt.layer setBorderWidth:1.0];
    [accountTxt.layer setBorderColor:[UIColor whiteColor].CGColor];
    [accountTxt.layer setMasksToBounds:YES];
    [accountTxt.layer setCornerRadius:20.0];
    [accountTxt setPlaceholder:@"手机号/邮箱"];
    [accountTxt setTextAlignment:NSTextAlignmentCenter];
    [accountTxt setTextColor:[UIColor lightGrayColor]];
    [accountTxt setFont:[UIFont fontWithName:nil size:18.0]];
    [accountTxt setTintColor:[UIColor lightGrayColor]];
    [self.contentView addSubview:accountTxt];
    [accountTxt setClearButtonMode:UITextFieldViewModeWhileEditing];
    accountTxt.delegate = self;
    [accountTxt setTag:0];
    
    pwdTxt = [[UITextField alloc]initWithFrame:CGRectMake(SCREENWIDTH/5/2, 104+15, SCREENWIDTH*4/5, 44)];
    [pwdTxt.layer setBorderWidth:1.0];
    [pwdTxt.layer setBorderColor:[UIColor whiteColor].CGColor];
    [pwdTxt.layer setMasksToBounds:YES];
    [pwdTxt.layer setCornerRadius:20.0];
    [pwdTxt setPlaceholder:@"请设置您的登录密码"];
    [pwdTxt setTextAlignment:NSTextAlignmentCenter];
    [pwdTxt setTextColor:[UIColor lightGrayColor]];
    [pwdTxt setFont:[UIFont fontWithName:nil size:18.0]];
    [pwdTxt setTintColor:[UIColor lightGrayColor]];
    [pwdTxt setSecureTextEntry:YES];
    [self.contentView addSubview:pwdTxt];
    pwdTxt.delegate = self;
    [pwdTxt setTag:1];
    
    confirmPwdTxt = [[UITextField alloc]initWithFrame:CGRectMake(SCREENWIDTH/5/2, (163+15), SCREENWIDTH*4/5, 44)];
    [confirmPwdTxt.layer setBorderWidth:1.0];
    [confirmPwdTxt.layer setBorderColor:[UIColor whiteColor].CGColor];
    [confirmPwdTxt.layer setMasksToBounds:YES];
    [confirmPwdTxt.layer setCornerRadius:20.0];
    [confirmPwdTxt setPlaceholder:@"请再次确认您的登录密码"];
    [confirmPwdTxt setTextAlignment:NSTextAlignmentCenter];
    [confirmPwdTxt setTextColor:[UIColor lightGrayColor]];
    [confirmPwdTxt setFont:[UIFont fontWithName:nil size:18.0]];
    [confirmPwdTxt setTintColor:[UIColor lightGrayColor]];
    [confirmPwdTxt setSecureTextEntry:YES];
    [self.contentView addSubview:confirmPwdTxt];
    confirmPwdTxt.delegate = self;
    [confirmPwdTxt setTag:2];
    
    registerBtn = [[UIButton alloc]initWithFrame:CGRectMake(SCREENWIDTH/5/2, 178+59, SCREENWIDTH*4/5, 44)];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setBackgroundColor:BULEHE];
    [registerBtn.layer setMasksToBounds:YES];
    [registerBtn.layer setCornerRadius:20.0];
    [registerBtn.titleLabel setFont:[UIFont fontWithName:nil size:18.0]];
    [registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    [registerBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
    [registerBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [registerBtn addTarget:self action:@selector(registerButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:registerBtn];
    
}
- (void)registerButtonAction:(id)sender {

    if ([accountTxt.text isEqualToString:@""]) {
        [[MViewUtil instance]showShortDurationMessage:@"! 请输入手机或邮箱" targetView:self.view];
    } else {
    
        if ([pwdTxt.text isEqualToString:@""]) {
            [[MViewUtil instance] showShortDurationMessage:@"! 请设置登录密码" targetView:self.view];
        }else {
        
            if ([confirmPwdTxt.text isEqualToString:@""]) {
                [[MViewUtil instance]showShortDurationMessage:@"! 请再次确认您的登录密码" targetView:self.view];
            }else {
            
                [[MRegistService sharedClient]getUserWithUsername:accountTxt.text password:pwdTxt.text completion:^(NSString *result){
                
                    [[MViewUtil instance] showShortDurationMessage:result targetView:self.view];
                    [self dismissViewControllerAnimated:YES completion:nil];
                } failure:^(NSString *error){
                
                    [[MViewUtil instance]showShortDurationMessage:error targetView:self.view];
                }];
            }
        }
    }
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    [accountTxt resignFirstResponder];
    [pwdTxt resignFirstResponder];
    [confirmPwdTxt resignFirstResponder];
}
#pragma mark 设置背景模糊
- (void)setBackgroundEffect {
    
    UIVisualEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleExtraLight];
    
    UIVisualEffectView *visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
    
    visualEffectView.frame = [[UIScreen mainScreen] bounds];
    
    [self.myImageView addSubview:visualEffectView];
}

// 背景图片隐藏
- (void)setBackground {

    CABasicAnimation *theAnimation;
    theAnimation=[CABasicAnimation animationWithKeyPath:@"opacity"];
    theAnimation.duration=1.0;
    theAnimation.fromValue=[NSNumber numberWithFloat:5.0];
    theAnimation.toValue=[NSNumber numberWithFloat:0.0];
    [self.myImageView.layer addAnimation:theAnimation forKey:@"animateOpacity"];
    
    [NSTimer scheduledTimerWithTimeInterval:theAnimation.duration
                                     target:self
                                   selector:@selector(targetMethod)
                                   userInfo:nil
                                    repeats:NO];
}
- (void)targetMethod {

    self.myImageView.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)backBtnAction:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
